﻿<?php
	require_once('config.php');
	function sendtoftp($file_source, $file_desc){
		$ftp_server = $GLOBALS['ftp_server'];
		$ftp_user_name = $GLOBALS['ftp_user_name'];
		$ftp_user_pass = $GLOBALS['ftp_user_pass'];
		$port = $GLOBALS['port'];
		$time_out = $GLOBALS['time_out'];
		$dir_ftp = $GLOBALS['dir_ftp'];
		//$tmp_name_ftp = rand(0,99999999999);
		
 		$conn_ftp = ftp_connect($ftp_server);
		if($conn_ftp){
			$login_ftp = ftp_login($conn_ftp, $ftp_user_name, $ftp_user_pass);
			if($login_ftp){
				//ftp_mkdir($conn_ftp, $dir_ftp);
				//echo "$dir_ftp/$file_desc";
				//echo "<br>$file_source";
				$sendfile = ftp_put($conn_ftp, "$dir_ftp/$file_desc", $file_source, FTP_BINARY);
				if($sendfile){
					return 'SENS';
				}else{
					return 'SENF';
				}
			}else{
				return 'LOGF';
			}
		}else{
			return 'CONF';
		}
		ftp_close($conn_ftp);
	}
	
	function getfromftp($file_ftp, $file_desc){
		$ftp_server = $GLOBALS['ftp_server'];
		$ftp_user_name = $GLOBALS['ftp_user_name'];
		$ftp_user_pass = $GLOBALS['ftp_user_pass'];
		$port = $GLOBALS['port'];
		$time_out = $GLOBALS['time_out'];
		$dir_ftp = $GLOBALS['dir_ftp'];
		$path = './FILEFTP/';
		$conn_ftp = ftp_connect($ftp_server);
		$tmp_file = fopen($file_desc,'w');
		if($conn_ftp){
			$login_ftp = ftp_login($conn_ftp, $ftp_user_name, $ftp_user_pass);
			if($login_ftp){
				$getfile = ftp_get($conn_ftp,$tmp_file,$dir_ftp.'/'.$file_ftp,FTP_ASCII);
				if($getfile){
					return 'GETS';
				}else{
					return 'GETF';
				}
			}else{
				return 'LOGF';
			}
		}else{
			return 'CONF';
		}
		ftp_close($conn_ftp);
		fclose($tmp_file);
	}
	
	function uploadfile($file){
		//echo $file['type'];
		$path = './FILEFTP/';
		//if ((($file['type'] == 'application/msword')||($file['type'] == 'application/vnd.ms-excel')||($file['type'] == 'text/plain')||($file['type'] == 'application/rar')||($file['type'] == 'application/octet-stream')||($file['type'] == 'application/x-zip-compressed'))&&($file['size'] <= 10000000)){
		if (($file['type'] == 'application/vnd.ms-excel')&&($file['size'] <= 10000000)){
			if ($file['error'] > 0){
				return 'FILF';
			}else{
				mkdir($path, 0777);
				if (file_exists($path . $file['name'])){
					//return 'FILEX';
					unlink($path . $file['name']);
		  		}
				move_uploaded_file($file['tmp_name'],$path . $file['name']);
				return 'FILS';
			}
		}else{
		 	return 'FILI';
	  	}
	}
	
	function fwrite_stream($fp, $string){
		for ($i=0; $i < strlen($string); $i += $fwrite){
			$fwrite = fwrite($fp, substr($string, $i));
			if (!$fwrite) {
				return $fwrite;
			}
		}
		return $i;
	}
?>